/**
 * 微信小程序腾讯地图SDK
 * 使用详情请参考腾讯地图官方文档：https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview
 */

class QQMapWX {
    constructor(options) {
        if (!options.key) {
            throw Error('key值不能为空');
        }
        this.key = options.key;
    }

    /**
     * POI周边检索
     *
     * @param {Object} options 接口参数对象
     *
     * 参数对象结构可以参考
     * @see https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview
     */
    search(options) {
        if (!options.keyword) {
            options.keyword = '景点';
        }

        const requestParams = {
            keyword: options.keyword,
            boundary: 'nearby(' + options.latitude + ',' + options.longitude + ',' + (options.radius || 1000) + ')',
            page_size: options.page_size || 10,
            page_index: options.page_index || 1,
            key: this.key
        };

        this.request(options, 'search', requestParams);
    }

    /**
     * 地点搜索
     *
     * @param {Object} options 接口参数对象
     *
     * 参数对象结构可以参考
     * @see https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview
     */
    getSuggestion(options) {
        if (!options.keyword) {
            throw Error('keyword参数不能为空');
        }

        const requestParams = {
            keyword: options.keyword,
            region: options.region || '全国',
            region_fix: options.region_fix || 0,
            policy: options.policy || 0,
            key: this.key
        };

        this.request(options, 'suggestion', requestParams);
    }

    /**
     * 距离计算
     *
     * @param {Object} options 接口参数对象
     *
     * 参数对象结构可以参考
     * @see https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview
     */
    calculateDistance(options) {
        if (!options.to) {
            throw Error('to参数不能为空');
        }
        const requestParams = {
            mode: options.mode || 'walking',
            to: options.to,
            key: this.key
        };

        // 计算距离接口需要传入from参数
        if (options.from) {
            requestParams.from = options.from;
        }

        this.request(options, 'distance', requestParams);
    }

    /**
     * 逆地址解析
     *
     * @param {Object} options 接口参数对象
     *
     * 参数对象结构可以参考
     * @see https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview
     */
    reverseGeocoder(options) {
        if (options.poi_options) {
            options.poi_options = JSON.stringify(options.poi_options);
        }

        const requestParams = {
            location: options.location || options.latitude + ',' + options.longitude,
            get_poi: options.get_poi || 0,
            poi_options: options.poi_options || '',
            key: this.key
        };

        this.request(options, 'reverseGeocoder', requestParams);
    }

    /**
     * 地址解析
     *
     * @param {Object} options 接口参数对象
     *
     * 参数对象结构可以参考
     * @see https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview
     */
    geocoder(options) {
        if (!options.address) {
            throw Error('address参数不能为空');
        }

        const requestParams = {
            address: options.address,
            region: options.region,
            key: this.key
        };

        this.request(options, 'geocoder', requestParams);
    }

    /**
     * 发起请求
     *
     * @param {Object} options 接口参数对象
     * @param {String} type 请求类型
     * @param {Object} requestParams 请求参数
     */
    request(options, type, requestParams) {
        const that = this;
        const apiUrls = {
            search: 'https://apis.map.qq.com/ws/place/v1/search',
            suggestion: 'https://apis.map.qq.com/ws/place/v1/suggestion',
            distance: 'https://apis.map.qq.com/ws/distance/v1/',
            reverseGeocoder: 'https://apis.map.qq.com/ws/geocoder/v1/',
            geocoder: 'https://apis.map.qq.com/ws/geocoder/v1/'
        };

        wx.request({
            url: apiUrls[type],
            data: requestParams,
            method: 'GET',
            header: {
                'content-type': 'application/json'
            },
            success(res) {
                if (res.statusCode === 200) {
                    if (type === 'reverseGeocoder' || type === 'geocoder') {
                        const data = res.data.result;
                        options.success && options.success(data);
                    } else {
                        options.success && options.success(res.data);
                    }
                } else {
                    options.fail && options.fail({
                        errMsg: `request failed with status code ${res.statusCode}`
                    });
                }
            },
            fail(error) {
                options.fail && options.fail(error);
            },
            complete(res) {
                options.complete && options.complete(res);
            }
        });
    }
}

export default QQMapWX;